package org.example.wumeng.controller;

import org.example.wumeng.pojo.Doctor;
import org.example.wumeng.service.T_doctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.example.wumeng.pojo.Result;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/doctors")
@CrossOrigin(origins = "*", maxAge = 3600)
public class T_doctorController {

    @Autowired
    private T_doctorService doctorService;

    /**
     * 分页查询医家列表
     */
    @GetMapping
    public Result<List<Doctor>> getList(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "speciality", required = false) String speciality) {
        try {
            name = (name != null && !name.trim().isEmpty()) ? name.trim() : null;
            speciality = (speciality != null && !speciality.trim().isEmpty()) ? speciality.trim() : null;

            Map<String, Object> result = doctorService.getList(page, size, name, speciality);
            @SuppressWarnings("unchecked")
            List<Doctor> doctors = (List<Doctor>) result.get("data");
            Long total = (Long) result.get("total");

            return new Result<>(200, "查询成功", doctors, total);

        } catch (IllegalArgumentException e) {
            return Result.badRequest(e.getMessage());
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("查询医家列表失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID获取医家详情
     */
    @GetMapping("/{id}")
    public Result<Doctor> getById(@PathVariable Long id) {
        try {
            Doctor doctor = doctorService.getById(id);
            return new Result<>(200, "查询成功", doctor, null);

        } catch (IllegalArgumentException e) {
            return Result.badRequest(e.getMessage());
        } catch (RuntimeException e) {
            return Result.notFound(e.getMessage());
        } catch (Exception e) {
            return Result.error("查询医家详情失败: " + e.getMessage());
        }
    }

    /**
     * 创建医家
     */
    @PostMapping
    public Result<Long> create(@RequestBody Doctor doctor) {
        try {
            Long id = doctorService.create(doctor);
            return new Result<>(200, "创建成功", id, null);

        } catch (IllegalArgumentException e) {
            return Result.badRequest(e.getMessage());
        } catch (RuntimeException e) {
            return Result.badRequest(e.getMessage());
        } catch (Exception e) {
            return Result.error("创建医家失败: " + e.getMessage());
        }
    }

    /**
     * 更新医家信息
     */
    @PutMapping("/{id}")
    public Result<Void> update(@PathVariable Long id, @RequestBody Doctor doctor) {
        try {
            doctorService.update(id, doctor);
            return new Result<>(200, "更新成功", null, null);

        } catch (IllegalArgumentException e) {
            return Result.badRequest(e.getMessage());
        } catch (RuntimeException e) {
            return Result.badRequest(e.getMessage());
        } catch (Exception e) {
            return Result.error("更新医家失败: " + e.getMessage());
        }
    }

    /**
     * 删除医家
     */
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        try {
            doctorService.delete(id);
            return new Result<>(200, "删除成功", null, null);

        } catch (IllegalArgumentException e) {
            return Result.badRequest(e.getMessage());
        } catch (RuntimeException e) {
            return Result.badRequest(e.getMessage());
        } catch (Exception e) {
            return Result.error("删除医家失败: " + e.getMessage());
        }
    }

    /**
     * 根据专长统计医家数量
     */
    @GetMapping("/count/speciality")
    public Result<Long> countBySpeciality(@RequestParam String speciality) {
        try {
            if (speciality == null || speciality.trim().isEmpty()) {
                return Result.badRequest("专长参数不能为空");
            }

            Long count = doctorService.countBySpeciality(speciality.trim());
            return new Result<>(200, "统计完成", count, null);

        } catch (Exception e) {
            return Result.error("统计医家数量失败: " + e.getMessage());
        }
    }

    /**
     * 根据专长查询医家
     */
    @GetMapping("/speciality/{speciality}")
    @CrossOrigin(origins = "*", maxAge = 3600)
    public Result<List<Doctor>> getBySpeciality(
            @PathVariable String speciality,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        try {
            if (speciality == null || speciality.trim().isEmpty()) {
                return Result.badRequest("专长参数不能为空");
            }
            return doctorService.getBySpeciality(speciality.trim(), page, size);
        } catch (Exception e) {
            return Result.error("查询专长医家失败: " + e.getMessage());
        }
    }

    /**
     * 获取专长统计
     */
    @GetMapping("/speciality/statistics")
    @CrossOrigin(origins = "*", maxAge = 3600)
    public Result<List<Map<String, Object>>> getSpecialityStatistics() {
        try {
            return doctorService.getSpecialityStatistics();
        } catch (Exception e) {
            return Result.error("获取专长统计失败: " + e.getMessage());
        }
    }
}
